var service_user = require("../../service/user.js");
var service_adv = require("../../service/adv.js");
var service_common = require("../../service/common.js");
var service_teacher = require("../../service/teacher.js");
var service_view = require("../../service/view.js");
const api = require('../../utils/util.js')
const service = require('../../utils/service.js')
const websocket = require('../../util/websocket.js')
var isAdmination = 0;
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    master_phone: "",
    msgList: 0,
    offon: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getPersonInfo();
    wx.setStorageSync("isred", true)
    this.TaskInit()
  },
  TaskInit:function(){
    
    getApp().socketEven = (res)=>{
      if (res.type == "login_back") {
        console.log("登录成功，获取首页消息未读数")
        this.getCount()
      } else if (res.type == "getNotReadCount_back") {
        console.log("获得消息未读数：" + res.data)
        this.setData({
          msgList: res.data
        })
      } else if (res.type == "single_chat_back") {
        console.log("收到消息，获取首页消息未读数")
        this.getCount()
      }
    }
  },
  chatInit: function() {
    // 用户登录
    websocket.send({
      appid: app.globalData.imID,
      type: 'login',
      uid: wx.getStorageSync('userInfo').openid,
      uid_name: wx.getStorageSync('userInfo').nickname,
      uid_head: wx.getStorageSync('userInfo').src
    })
    // 监听消息
    // if (app.globalData.socket!=''){
    //   wx.closeSocket()
    //   this.taskInit()
    // }else{
    //   this.taskInit()
    // }
  },
  // 获取消息数量
  getCount: function() {
    websocket.send({
      "appid": getApp().globalData.imID,
      "type": "get_not_read_count",
      "uid": wx.getStorageSync('userInfo').openid
    }, this.chatInit)
  },
  // 获取店铺信息
  getShopInfo: function(shop_user_id) {
    let that = this
    let url = service.url('getShopDetail')
    api.ajax(url, {
      userid: shop_user_id
    }, function(res) {
      console.log(res);
      if (res.code == 0) {
        wx.setStorage({
          key: 'shopInfo',
          data: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },
  shopGetUserShopInfo: function(out_user_id) {
    api.ajax(service.url('getUserShopInfo'), {
      userid: out_user_id
    }, function(res) {
      wx.setStorageSync('shop_data_shop', res.data);
    })
  },
  // 前往模板选择
  // goTel:function(){
  //   wx.navigateTo({
  //     url: '/pages/template/template',
  //   })
  // },
  // 获取店铺的unid
  getShop_uid: function(cb) {
    api.ajax(service.url('getShop_enter'), {
        userid: getApp().globalData.userInfo.id
      },
      function(res) {
        if (res.code == 0) {
          wx.setStorageSync("shop_data", res.data)
          cb()
        } else {
          api.toast(res.message)
        }
      })
  },

  //图片预览
  showPhoto: function(e) {
    var id = e.currentTarget.id;
    id = parseInt(id);
    var array = [];
    for (var i = 0; i < this.data.userInfo.imgs.length; i++) {
      array[i] = this.data.userInfo.imgs[i].b;
    }
    wx.previewImage({
      current: array[id],
      urls: array
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let userInfo = wx.getStorageSync('userInfo')
    service_common.fetchData(
      service_common.getUrl("user.getUserMpInfo"), {
        openid: getApp().globalData.userInfo.openid
      },
      (res) => {
        if (res.data.code == 0) {
          wx.setStorageSync('userInfo', res.data.data);
          app.globalData.userInfo = res.data.data
          app.globalData.openid = res.data.data.openid
          app.globalData.unionid = res.data.data.unionid
          this.chatInit()
          // if (this.data.offon) {
          //   this.chatInit()
          // } else {
          //   this.getCount()
          // }
          this.setData({
            userInfo: res.data.data,
            offon: false
          })
          this.getShop_uid(() => {
            api.ajax(service.url('getConstShopUser'), {}, res => {
              wx.setStorageSync('out_user_id', res.data.shop_user_id)
              this.shopGetUserShopInfo(res.data.shop_user_id);
              this.getShopInfo(res.data.shop_user_id)
            })
          })

        }
      },
      "GET"
    )
  },

  // 触底
  onPullDownRefresh: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    // 设置转发的路径
    var share_path = 'pages/auth/auth?webUrl=other&zf_openid=' + app.globalData.userInfo.openid + "&r=" + Math.random(100000);
    console.log(share_path)
    return {
      title: "我是" + getApp().globalData.userInfo.nickname + getApp().globalData.share_title,
      desc: '一个名片社交的资源配对平台，找资源找合作你就来',
      path: share_path,
      success: function() {
        service_user.setUserShareTime(function(res) {
          if (res.data.code == 0) {
            wx.showToast({
              title: '递名片成功',
              icon: 'success',
              duration: 1000,
            });
            setTimeout(function() {
              wx.showToast({
                title: '获得一天展示权限！',
                icon: 'success',
                duration: 1000,
              });
            }, 1000)
          } else {
            wx.showToast({
              title: '递名片成功',
              icon: 'success',
              duration: 1500
            });
          }
        })
      },
      fail: res => {
        wx.showToast({
          title: '递名片失败',
          image: '../../image/cancel.png',
          duration: 2000
        });
      }
    }
  },
  putHongBao: function() {
    wx.navigateToMiniProgram({
      appId: 'wx36e289fcc22707ec',
      path: "pages/index/index?openid=" + getApp().globalData.userInfo.openid + "&from=yzmp",
    })
  },
  //--获取自己的信息
  getPersonInfo: function() {
    /* service_common.fetchData(service_common.getUrl("user.getUserMpInfo"), {
        uniq_openid: ''
    },function (result) {
        console.log(result);
        if (result.data.code == 0) {
            
        } else {
            
        }
    },"GET"); */
  },
  // 设置formId
  setFormId: function(e) {
    console.log(e);
    //service_user.addFormId(form_id);
  },
  // 介绍-查看大图
  introduceBigImg: function(e) {
    let index = e.currentTarget.dataset.index;
    let array = [];
    let _this = this;
    for (let i = 0; i < this.data.userInfo.imgs.length; i++) {
      array.push(this.data.userInfo.imgs[i].b);
    }
    wx.previewImage({
      current: _this.data.userInfo.imgs[+index].b,
      urls: array
    })
  },
  // 前往雷达
  goLd: function() {
    // wx.navigateTo({
    //   url: '/pages/ld_list/ld_list',
    // })
    wx.navigateTo({
      url: '/pages/ld_win/ld_win',
    })
  },
  // 发名片
  sendCard: function() {
    if (!wx.canIUse('button.open-type.share')) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请点击右上角转发。',
        showCancel: false
      })
    }
  },
  // 编辑名片
  toEdit: function(e) {
    wx.navigateTo({
      url: '/pages/edit/edit'
    });
  },
  // 前往官网
  toGw: function(e) {
    wx.navigateTo({
      url: '/pages/gwang/index/index',
    })
  },
  // 人气列表
  goSeeView: function() {
    wx.navigateTo({
      url: '/pages/see_love/see_love'
    })
  },
  // 点赞列表
  goLoveList: function(e) {
    wx.navigateTo({
      url: '/pages/see_view/see_view'
    })
  },
  // 收藏列表与前往名片夹
  goCollectList: function() {
    wx.navigateTo({
      url: '/pages/see_collect/see_collect'
    })
  },
  goAddress: function() {
    wx.navigateTo({
      url: '/pages/address/address'
    })
  },
  // 个人二维码
  goPersonCode: function() {
    service_user.getUserCode();
  },

  // 一周年活动页
  goActivity: function() {
    wx.navigateTo({
      url: '/pages/activity/activity?is_me=1',
    })
  }
})